/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package eu.fastl.dragster.web.filter;

import java.io.IOException;
import java.io.Serializable;
import java.util.Locale;

import javax.servlet.ServletResponse;

/**
 * Class which stores the info of a ServletResponse
 *
 * @author Ernst Fastl
 */
public class CachedServletResponse implements Serializable {
    private static final long serialVersionUID = -5618402258315884442L;
    private final String contentType;
    private final String characterEncoding;
    private final Locale locale;
    private final byte[] content;

    public CachedServletResponse(ServletResponse response, byte[] content) {
        this.content = content;
        this.contentType = response.getContentType();
        this.characterEncoding = response.getCharacterEncoding();
        this.locale = response.getLocale();
    }

    public void writeCachedResponse(ServletResponse response) throws IOException {
        response.setCharacterEncoding(this.characterEncoding);
        response.setContentType(this.contentType);
        response.setLocale(this.locale);
        response.setContentLength(this.getContent().length);
        response.getOutputStream().write(this.getContent());
    }

	public byte[] getContent() {
		return content;
	}
}
